package com.example.music_impl;

import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.music_api.bean.AudioInfo;

import java.util.List;

import cc.common.base.BaseFragment;
import cc.common.base.BaseObserver;
import com.example.music_impl.presenter.MusicListPresenter;
import com.example.music_impl.view.IMusicView;

public class MusicListFragment extends BaseFragment implements IMusicView {
    RecyclerView recyclerView;
    MusicListPresenter presenter = new MusicListPresenter(this);
    private BaseQuickAdapter<AudioInfo, BaseViewHolder> adapter;

    @Override
    protected int onGetLayoutId() {
        return R.layout.fragment_music_list;
    }

    @Override
    protected void onViewCreatedInit(View view) {
        recyclerView = view.findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter = new BaseQuickAdapter<AudioInfo, BaseViewHolder>(android.R.layout.simple_list_item_1) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, AudioInfo item) {
                helper.setText(android.R.id.text1, item.getName());
            }
        });

        presenter.getMusicList().compose(apply()).subscribe(new BaseObserver<List<AudioInfo>>(this) {
            @Override
            protected void doNext(List<AudioInfo> audioInfos) {
                adapter.setNewData(audioInfos);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
